#include <reg52.h>
#include "lcd1602.h"

#define LCD_CLEAR 0x01
#define LCD_Display_Mode 0X38

#define DISPLAY_OFF 0x08
#define DISPLAY_ON_NO_CURSOR 0x0c
#define DISPLAY_ON_WITH_CURSOR_NO_BLINK 0x0e
#define DISPLAY_ON_WITH_CURSOR_BLINK 0x0f

#define AUTO_BACK_STEP 0x04
#define AUTO_NEXT_STEP 0x06
#define AUTO_DISPLAY_MOVE_LEFT 0x07
#define AUTO_DISPLAY_MOVE_RIGHT 0x05

#define ALL_MOVE_LEFT 0x18
#define ALL_MOVE_RIGHT 0x1c
#define CURSOR_MOVE_LEFT 0x10
#define CURSOR_MOVE_RIGHT 0x14

#define FIRST_ROW 0x80
#define SECOND_ROW FIRST_ROW+0x40

sbit enable = P2^7;
sbit RS = P2^6;
sbit RW = P2^5;

void delay(unsigned int z)
{
    unsigned int x,y;
    for(x=z;x>0;x--)
        for(y=220;y>0;y--);
}

void LCD_writeCmd(unsigned char cmd){
    RS = 0;
    P0 = cmd;
    delay(5);
    enable = 1;
    delay(5);
    enable = 0;
}

void LCD_writeData(unsigned char dat)
{
    RS = 1;
    P0 = dat;
    delay(5);
    enable = 1;
    delay(5);
    enable = 0;
}

void LCD_writeLine(unsigned char *line){
		unsigned char tttt = line[1];
    unsigned char i=0;
    BOOL flag = 0;
    LCD_writeCmd(LCD_CLEAR);  //?????????,??????????????
    LCD_writeCmd(FIRST_ROW);
    while(line[i] != '\0'){
        LCD_writeData(line[i++]);
        if(i>15 && flag == 0){
            LCD_writeCmd(SECOND_ROW);
            flag = 1;
        }
        delay(5);
    }
}

void LCD_init()
{
    RW = 0;
    enable = 0;
    LCD_writeCmd(LCD_Display_Mode);
    LCD_writeCmd(DISPLAY_ON_NO_CURSOR);
    LCD_writeCmd(AUTO_NEXT_STEP);
    LCD_writeCmd(LCD_CLEAR);
}

